<template>
    <div class="white-box">
        <div class="task-one">
            <div class="task-one__item">
                <span class="task-one__label">商家名称：</span>
                <el-input
                    placeholder="请输入商家名称"
                    v-model="state.storeName"
                    @keyup.enter.native="handleSearch"
                    clearable
                >
                </el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">法人姓名：</span>
                <el-input
                    placeholder="请输入法人姓名"
                    v-model="state.personName"
                    @keyup.enter.native="handleSearch"
                    clearable
                >
                </el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">手机号码：</span>
                <el-input
                    placeholder="请输入手机号码"
                    v-model.trim="state.personPhone"
                    max-length="11"
                    clearable
                    @keyup.enter.native="handleSearch"
                ></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">审核状态：</span>
                <el-select
                    v-model="state.approval"
                    placeholder="请选择审核状态"
                    clearable
                >
                    <el-option
                        v-for="item in state.approvalList"
                        :key="item.memberKey"
                        :label="item.memberName"
                        :value="item.memberKey"
                    />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">商家状态：</span>
                <el-select
                    v-model="state.status"
                    placeholder="请选择状态"
                    clearable
                >
                    <el-option
                        v-for="item in state.statusList"
                        :key="item.memberKey"
                        :label="item.memberName"
                        :value="item.memberKey"
                    />
                </el-select>
            </div>
            <div class="task-one__btn clearfix">
                <el-button
                    type="primary"
                    :loading="loading"
                    icon="Search"
                    @click="handleSearch"
                    >搜索</el-button
                >
            </div>
        </div>

        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            row-key="sid"
            :header-cell-style="{
                background: '#F5F7FA',
                textAlign: 'center',
            }"
            :cell-style="
                ({ column }) => {
                    return {
                        textAlign:
                            column.className === 'operation-nav'
                                ? 'left'
                                : 'center',
                        paddingLeft:
                            column.className === 'operation-nav' ? '25px' : '0',
                    };
                }
            "
            border
            element-loading-background="rgba(255, 255, 255, 0.6)"
            @selection-change="selectionChange"
            @sort-change="sortChange"
        >
            <el-table-column
                prop="storeName"
                label="商家名称"
                min-width="150"
                fixed="left"
                sortable="custom"
            ></el-table-column>
            <!-- <el-table-column
                prop="taxNumber"
                label="纳税识别号"
                min-width="120"
                sortable="custom"
            ></el-table-column> -->
            <!-- <el-table-column
                prop="accountName"
                label="开户人"
                min-width="150"
                sortable="custom"
            ></el-table-column> -->
            <el-table-column
                prop="personName"
                label="法人"
                min-width="150"
                sortable="custom"
            ></el-table-column>
            <el-table-column
                prop="personPhone"
                label="手机号"
                min-width="150"
                sortable="custom"
            ></el-table-column>
            <!-- <el-table-column label="营业执照" min-width="100">
                <template #default="{ row }">
                    <Image :src="row.businessImgUrl" />
                </template>
            </el-table-column> -->
            <el-table-column label="审核状态" min-width="120" sortable="custom">
                <template #default="{ row }">
                    {{
                        dictStore.getDictMemberName(
                            "store_approval",
                            row.approval
                        )
                    }}
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                min-width="100"
                sortable="custom"
            >
                <template #default="{ row }">
                    {{ dictStore.getDictMemberName("data_status", row.status) }}
                </template>
            </el-table-column>
            <el-table-column
                prop="created"
                label="创建时间"
                min-width="160"
                sortable="custom"
            ></el-table-column>
            <el-table-column
                label="操作"
                width="200"
                class-name="operation-nav"
                fixed="right"
            >
                <template #default="{ row }">
                    <MoreBtn
                        :buttons="[
                            {
                                props: {
                                    onClick: () => onClickDetails(row.sid),
                                    'v-hasPermi': ['enttp:store:query'],
                                },
                                text: '详情',
                            },
                            // {
                            //     props: {
                            //         onClick: () => changeStatus(row.sid),
                            //     },
                            //     text: ['停用', '启用'][row.status],
                            // },
                            {
                                props: {
                                    onClick: () => examine(row.sid),
                                    'v-hasPermi': ['enttp:store:approval'],
                                },
                                text: '审核',
                                isRender: row.approval === 0,
                            },
                            // {
                            //     props: {
                            //         onClick: () => onClickDel(row.sid),
                            //     },
                            //     text: '删除',
                            // },
                        ]"
                    />
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="slot, sizes, prev, pager, next, jumper"
        >
            <span class="color-#606266"
                >共&nbsp;{{
                    totalNums >= 100 * pageSize
                        ? `${100 * pageSize}+`
                        : totalNums
                }}&nbsp;条</span
            >
        </el-pagination>
    </div>

    <!-- 审核 -->
    <el-dialog v-model="state.dialogVisible" title="审核" width="350">
        <el-form
            ref="ruleRef"
            :model="state.ruleForm"
            :rules="state.rules"
            label-position="top"
        >
            <el-form-item label="状态" prop="approval">
                <el-radio-group v-model="state.ruleForm.approval">
                    <el-radio
                        v-for="item in state.approvalList.filter(
                            (item) => item.memberKey !== 0
                        )"
                        :key="item.memberKey"
                        :value="item.memberKey"
                        >{{ item.memberName }}</el-radio
                    >
                </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="note">
                <el-input
                    v-model.trim="state.ruleForm.note"
                    type="textarea"
                    maxlength="100"
                    :autosize="{ minRows: 2 }"
                    clearable
                    show-word-limit
                    placeholder="请输入备注"
                />
            </el-form-item>
        </el-form>
        <el-form-item>
            <el-button
                type="primary"
                :loading="form_loading"
                class="w-100% mt-10 mb--10"
                @click="submitForm(ruleRef)"
            >
                确定
            </el-button>
        </el-form-item>
    </el-dialog>

    <el-drawer v-model="state.showDetail" title="商家详情" size="540">
        <template v-if="detail">
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">商家名称</el-col>
                <el-col :span="18">{{ detail.storeName }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">纳税识别号</el-col>
                <el-col :span="18">{{ detail.taxNumber }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">开户人</el-col>
                <el-col :span="18">{{ detail.accountName }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">开户账号</el-col>
                <el-col :span="18">{{ detail.accountBankNumber }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">开户行</el-col>
                <el-col :span="18">{{ detail.accountBank }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">对公账户</el-col>
                <el-col :span="18">{{ detail.accountNumber }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">法人姓名</el-col>
                <el-col :span="18">{{ detail.personName }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">法人身份证</el-col>
                <el-col :span="18">{{ detail.personNumber }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">法人手机号</el-col>
                <el-col :span="18">{{ detail.personPhone }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">营业执照</el-col>
                <el-col :span="18">
                    <Image :size="100" :src="detail.businessImgUrl" />
                </el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">审核状态</el-col>
                <el-col :span="18">{{
                    dictStore.getDictMemberName(
                        "store_approval",
                        detail.approval
                    )
                }}</el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="6" align="end" class="mb-20">商家状态</el-col>
                <el-col :span="18">{{
                    dictStore.getDictMemberName("data_status", detail.status)
                }}</el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="6" align="end" class="mb-20">创建时间</el-col>
                <el-col :span="18">{{ detail.created }}</el-col>
            </el-row>
        </template>
    </el-drawer>
</template>

<script setup>
import { inject, reactive, onMounted, onActivated, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import OneLoad from "@mixins/one-load";
import MoreBtn from "@/components/MoreBtn.vue";
import { ElMessageBox } from "element-plus";
import Image from "@/components/Image.vue";
import {
    getStores,
    getStoreDetail,
    delStores,
    changeStoreStatus,
    approvalStore,
} from "@/apis/store/store";
import { useDictStore } from "@stores/dict.js";

const dictStore = useDictStore();
dictStore.getDictMembers("data_status").then((res) => (state.statusList = res));
dictStore
    .getDictMembers("store_approval")
    .then((res) => (state.approvalList = res));

const router = useRouter();
const $message = inject("$message");

const form_loading = ref(false);
const ruleRef = ref(null);
const state = reactive({
    statusList: [],
    approvalList: [],
    ascended: false,
    sortFieldName: null,
    personName: null,
    personPhone: null,
    approval: null,
    storeName: null,
    status: null,
    dialogVisible: false,
    ruleForm: {
        approval: 1,
        note: null,
        sid: null,
    },
    rules: {
        approval: [
            { required: true, message: "请选择审核状态", trigger: "blur" },
        ],
    },
    sidList: [],
    showDetail: false,
    detail: null,
});

const { detail } = toRefs(state);

onActivated(() => {
    pubParams(getList);
});

/**
 * minxin列表加载
 */
const {
    loading,
    arr_data,
    pubRefreshParams,
    pubParams,
    page,
    pageSize,
    getdata,
    pageSizeNum,
    totalNums,
    openPaging,
} = OneLoad();

openPaging(getList);

function getList() {
    getdata(
        getStores({
            current: page.value,
            size: pageSize.value,
            ascended: state.ascended,
            sortFieldName: state.sortFieldName,
            personName: state.personName,
            personPhone: state.personPhone,
            approval: state.approval,
            storeName: state.storeName,
            status: state.status,
        })
    );
}

// 搜索
const handleSearch = () => {
    pubRefreshParams(getList);
};

// 排序
const sortChange = (e) => {
    const { property, order } = e.column;
    state.ascended = order === "ascending";
    state.sortFieldName = order ? property : null;
    pubRefreshParams(getList);
};

// 跳转详情
const onClickDetails = (sid) => {
    state.showDetail = true;
    state.detail = null;
    getDetail(sid);
};

const getDetail = async (sid) => {
    const res = await getStoreDetail({ sid });
    if (res.data.code == 200) {
        state.detail = res.data.data;
    } else {
        $message({ message: res.data.msg, type: "error" });
    }
};

// 点击启用/停用按钮
const changeStatus = async (sid) => {
    const res = await changeStoreStatus({ sid });
    if (res.data.code == 200) {
        $message({ message: "操作成功", type: "success" });
        pubParams(getList);
    } else {
        $message({ message: res.data.msg, type: "error" });
    }
};

// 选中项改变
const selectionChange = (e) => {
    state.sidList = e.map((item) => item.sid);
};

// 删除
const onClickDel = async (sid) => {
    if (sid) {
        state.sidList = [sid];
    }
    if (!state.sidList.length) {
        return $message({ message: "请先勾选要删除的数据", type: "warning" });
    }
    await ElMessageBox.confirm("确定删除商家吗？");
    const res = await delStores({ sidList: state.sidList });
    if (res.data.code == 200) {
        $message({ message: "删除成功", type: "success" });
        pubParams(getList);
    } else {
        $message({ message: res.data.msg, type: "error" });
    }
};

// 审核
const examine = (sid) => {
    state.dialogVisible = true;
    state.ruleForm.sid = sid;
};

// 提交表单
const submitForm = (formEl) => {
    if (!formEl) return;
    formEl.validate(async (valid, fields) => {
        if (valid) {
            form_loading.value = true;
            const res = await approvalStore(state.ruleForm);
            form_loading.value = false;
            state.dialogVisible = false;
            if (res.data.code == 200) {
                $message({ message: "操作成功", type: "success" });
                pubRefreshParams(getList);
            } else {
                $message({ message: res.data.msg, type: "error" });
            }
        }
    });
};
</script>

<style lang="less" scoped></style>
